package com.dts.project.dao;
import java.sql.*; 
import com.dts.core.dao.AbstractDataAccessObject;
import com.dts.core.util.CoreList;
import com.dts.core.util.DateWrapper;
import com.dts.core.util.LoggerManager;
import com.yourcompany.struts.form.MobileCardDetailsForm;

public class MobileCardDetailsDao extends AbstractDataAccessObject 
{
	public boolean insertMobileCardDetails(MobileCardDetailsForm mcform)
	{
		java.sql.Connection con=null;
		boolean flag=false;
		try
		{
		int n=getSequenceID("MobileCardDetails","cardId");
		con=getConnection();
		PreparedStatement ps=con.prepareStatement("insert into MobileCardDetails values(?,?,?,?,?,?,?)");
		ps.setInt(1,n);
		ps.setInt(2,mcform.getOperationId());
		ps.setString(3,mcform.getCardName());
		ps.setInt(4,mcform.getCardAmount());
		ps.setString(5,mcform.getCardNumber());
		ps.setString(6,DateWrapper.parseDate(mcform.getValidityDate()));
		ps.setString(7,mcform.getCardStatus());
		 
		int i=ps.executeUpdate();
		if(i>0)
			flag=true;
		 
			
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}finally
	{
		try{
			con.close();
		}
	catch(Exception e)
	{
		LoggerManager.writeLogInfo(e);
	}
	}
	return flag;
	
	
		}

public CoreList ViewMobileCardDetails()
{   CoreList v=new CoreList();
	java.sql.Connection con=null;
	MobileCardDetailsForm mcform=null;
	try
	{
		con=getConnection();
		Statement st=con.createStatement();
		ResultSet rs=st.executeQuery("select md.cardid,md.operationid,md.cardname,md.cardamount,md.cardnumber,md.validitydate,md.cardstatus,mo.operatorname from mobilecarddetails md,mobileoperatormaster mo where md.operationid=mo.operatorid");
		 
		while(rs.next())
		{
			mcform=new MobileCardDetailsForm();
			mcform.setCardId(rs.getInt(1));
			mcform.setOperationId(rs.getInt(2));
			mcform.setCardName(rs.getString(3));
			mcform.setCardAmount(rs.getInt(4));
			mcform.setCardNumber(rs.getString(5));
			mcform.setValidityDate(DateWrapper.parseDate(rs.getDate(6)));
			mcform.setCardStatus(rs.getString(7));		
		mcform.setOperatorname(rs.getString(8));	
            v.add(mcform);
		}

		 
	}
		catch(Exception e)
	{
		e.printStackTrace();
	}
		finally
		{
			try{
				con.close();
			}
		catch(Exception e)
		{
			LoggerManager.writeLogInfo(e);
		}
		}
		return v;
		
	
}
public MobileCardDetailsForm ViewMobileCardDetailsById(int cardId)
{   MobileCardDetailsForm mcform=new MobileCardDetailsForm();
	java.sql.Connection con=null;
	 
	try
	{
		 
		con=getConnection();
		 
		PreparedStatement ps=con.prepareStatement("select md.cardid,md.operationid,md.cardname,md.cardamount,md.cardnumber,md.validitydate,md.cardstatus,mo.operatorname from mobilecarddetails md,mobileoperatormaster mo where md.operationid=mo.operatorid and cardId = ? ");
		ps.setInt(1,cardId);
		ResultSet rs=ps.executeQuery();
		 
		if(rs.next())
		{
			mcform=new MobileCardDetailsForm();
			mcform.setCardId(rs.getInt(1));
			mcform.setOperationId(rs.getInt(2));
			mcform.setCardName(rs.getString(3));
			mcform.setCardAmount(rs.getInt(4));
			mcform.setCardNumber(rs.getString(5));
			mcform.setValidityDate(DateWrapper.parseDate(rs.getDate(6)));
			mcform.setCardStatus(rs.getString(7));		
		mcform.setOperatorname(rs.getString(8));	
		
		}

		 
	}
		catch(Exception e)
	{
		
	}finally
	{
		try{
			con.close();
		}
	catch(Exception e)
	{
		LoggerManager.writeLogInfo(e);
	}
	}
	return mcform;
	
	
}

public boolean deleteMobileCardDetails(int cardId)
	{
		java.sql.Connection con=null;
		boolean flag=false;
		try
		{
			 
			con=getConnection();
			 
			PreparedStatement ps=con.prepareStatement("delete from MobileCardDetails where cardId = ? ");
			ps.setInt(1,cardId);
			int i=ps.executeUpdate();
			if(i>0)
				flag=true;
			
			          
		}
		catch(Exception e)
		{
		}
		finally
		{
			try{
				con.close();
			}
		catch(Exception e)
		{
			LoggerManager.writeLogInfo(e);
		}
		}
		return flag;
		
		}

public boolean UpdateMobileCardDetails(MobileCardDetailsForm mcform)
{
	java.sql.Connection con=null;
	boolean flag=false;
	try
	{
	 
	con=getConnection();
	PreparedStatement ps=con.prepareStatement("update MobileCardDetails set operationid = ?,cardname = ?,cardamount = ?, cardnumber= ?,validitydate = ?,cardstatus = ? where cardId = ? ");
	ps.setInt(7,mcform.getCardId());
	ps.setInt(1,mcform.getOperationId());
	ps.setString(2,mcform.getCardName());
	ps.setInt(3,mcform.getCardAmount());
	ps.setString(4,mcform.getCardNumber());
	ps.setString(5,DateWrapper.parseDate(mcform.getValidityDate()));
	ps.setString(6,mcform.getCardStatus());
	int i=ps.executeUpdate();
	if(i>0)
		flag=true;
	 
}
catch(Exception e)
{
}finally
{
	try{
		con.close();
	}
catch(Exception e)
{
	LoggerManager.writeLogInfo(e);
}
}
return flag;


	}

public int getCardNumber(int cardid)
{
	Connection con=null;
	int cardnumber=0;
	try{
		con=getConnection();
		PreparedStatement pst=con.prepareStatement("select cardnumber from MobileCardDetails where cardid=?");
pst.setInt(1,cardid);
ResultSet rs=pst.executeQuery();
while(rs.next())
{
	cardnumber=rs.getInt(1);
}
	}catch (Exception e) {
		// TODO: handle exception
	}
	return cardnumber;
}
}






 


 


 
